function Get-AzureLocation {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $false)][string]$Location,
        [Parameter(Mandatory = $false)][string]$ResourceGroup,
        [Parameter(Mandatory = $false)][bool]$ConfirmAll = $false
    )

    # Allow user to use existing value or to update it.
    if ((Confirm-SingleValue -Description 'Azure location' -Value $Location -ConfirmAll $ConfirmAll) -eq $true) { return $Location }

    $ResGroup = Get-AzResourceGroup -Name $ResourceGroup
    
    $svparams = @{
        Description = "You can use the location of your Azure resource group as the location for your SQL server and database.
Azure resource group location"
        Value = $ResGroup.Location
    }
    
    if(Confirm-SingleValue @svparams)
    {
        return $ResGroup.Location
    }
    
    Write-Host # Newline
    Write-Host 'Checking for locations allowed on your subscription. This might take a few minutes...'
    Write-Host # Newline
    
    # Get all locations that have the providers we need
    $TestLocations = Get-AzLocation | Where-Object Providers -eq 'Microsoft.DataFactory' | Where-Object Providers -eq 'Microsoft.sql' | Where-Object Providers -eq 'Microsoft.Storage'
    $AzureLocations = $TestLocations.Location
    
    #Create variables
    $AllowedLocations = @()
    
    # Check if we can shorten the list to only contain the allowed locations that this subscription/resource group allows
    $AzureLocations | ForEach-Object { 
        
        $Random = Get-Random -Minimum 1 -Maximum 99999
        $Random2 = Get-Random -Minimum 1 -Maximum 99999
        $ADFv2Name = 'LSR-loctestji-deletethis-'+ $Random +'-'+ $Random2;
        
        try {
            # Try creating a simple data factory and catch if the creation returns error.
            Set-AzDataFactoryV2 -ResourceGroupName $Parameters.Azure.ResourceGroup -Name $ADFv2Name -Location $_
            $AllowedLocations += $_
            # Clean up of the test data factories created
            Remove-AzDataFactoryV2 -ResourceGroupName $Parameters.Azure.ResourceGroup -Name $ADFv2Name -Force    
        }
        catch {
            #Set function returns error because ADF could not be created so it is not added to allowed locations.
        }    
    } | Out-Null
    
    $params = @{
        Question = 'Please select location for your Azure resources:'
        Options = $AllowedLocations
    }
    
    return Open-LSQuestion @params
}